<?php

/**
 * Projects maintainence controller.
 *
 */
class Projects_Controller extends SPF_Controller {

   /**
    * The version number of the controller.
    * @var     string
    */
   const VERSION = '1.0.0';

   /**
    * The author of the controller.
    * @var     string
    */
   const AUTHOR  = 'Simon Downes';
   
   /**
    * Constructor
    * @return       void
    */
   public function __construct() {
      /* construct the parent object */
      parent::__construct(__CLASS__);
   }

   /**
    * Destructor
    * @access       public
    * @return       void
    */
   public function __destruct() {
   }

   /**
    * Determines if the current user is permitted to access this controller and perform the requested action
    * @return       boolean   true if access is allowed, false otherwise.
    */
   public function authenticate() {
   
      return (SPF::$session->user != NULL);
      
   } // authenticate
   
   public function index() {
      
      $this->assign('projects', Project::browse_data());
      
      $this->display('browse');
      
   } // browse
   
   public function edit( $id = NULL ) {
      
      // load the requested project record
      $project = new Project();
      $project->fetch( (int) $id );

      if( $id && !$project->id ) {
         SPF::flash(SiTrack::MSG_NOT_FOUND, 'error');
         SPF::redirect('projects');
      }
      
      if( SPF::form_submitted('frm_project') ) {
         
         // run html_entity_decode() against each element in the post array to make sure we're not
         // inserting html into the database. this is only an issue when re-saving existing records
         // as when output to the browser the data will have html entities encoded and we don't want
         // to be saving the encoded entities into the database...
         $data = array_map('html_entity_decode', SPF::post());
         $project->from_array($data);
         
         $project->validate();
         
         // if no errors then assign values to dataobject and update
         if( !$project->has_errors() ) {
            $project->update(false);
            SPF::flash(SiTrack::MSG_SAVED, 'success');
            SPF::redirect('projects');
         }

         SPF::flash(SiTrack::MSG_ERRORS, 'error');
         
      } // is_submitted

      $this->assign('project', $project->to_array());
      $this->assign('components', $project->components_data());
      $this->assign('milestones', $project->milestones_data());
      
      $this->display('edit');

   } // edit
   
   public function edit_component( $project_id = NULL, $id = NULL ) {
      
      $project = new Project();
      $project->fetch((int) $project_id);
      
      if( !$project->id ) {
         SPF::flash(SiTrack::MSG_NOT_FOUND, 'error');
         SPF::redirect('projects');
      }
      
      // load the requested project record
      $component = new Component();
      $component->fetch( (int) $id );

      if( $id && !$component->id ) {
         SPF::flash(SiTrack::MSG_NOT_FOUND, 'error');
         SPF::redirect('projects', 'edit', $project->id);
      }
      
      if( SPF::form_submitted('frm_component') ) {
         
         // run html_entity_decode() against each element in the post array to make sure we're not
         // inserting html into the database. this is only an issue when re-saving existing records
         // as when output to the browser the data will have html entities encoded and we don't want
         // to be saving the encoded entities into the database...
         $data = array_map('html_entity_decode', SPF::post());
         $component->from_array($data);
         $component->project_id = $project->id;
         
         $component->validate();
         
         // if no errors then assign values to dataobject and update
         if( !$component->has_errors() ) {
            $component->update(false);
            SPF::flash(SiTrack::MSG_SAVED, 'success');
            SPF::redirect('projects', 'edit', $project->id);
         }

         SPF::flash(SiTrack::MSG_ERRORS, 'error');
         
      } // is_submitted
      
      $this->assign('project', $project->to_array());
      $this->assign('component', $component->to_array());
      
      $this->display('edit_component');

   } // edit_component
   
   public function edit_milestone( $project_id = NULL, $id = NULL ) {
      
      $project = new Project();
      $project->fetch((int) $project_id);
      
      if( !$project->id ) {
         SPF::flash(SiTrack::MSG_NOT_FOUND, 'error');
         SPF::redirect('projects');
      }
      
      // load the requested project record
      $milestone = new Milestone();
      $milestone->fetch( (int) $id );

      if( $id && !$milestone->id ) {
         SPF::flash(SiTrack::MSG_NOT_FOUND, 'error');
         SPF::redirect('projects', 'edit', $project->id);
      }
      
      if( SPF::form_submitted('frm_milestone') ) {
         
         // run html_entity_decode() against each element in the post array to make sure we're not
         // inserting html into the database. this is only an issue when re-saving existing records
         // as when output to the browser the data will have html entities encoded and we don't want
         // to be saving the encoded entities into the database...
         $data = array_map('html_entity_decode', SPF::post());
         $milestone->from_array($data);
         $milestone->project_id = $project->id;
         
         $milestone->validate();
         
         // if no errors then assign values to dataobject and update
         if( !$milestone->has_errors() ) {
            $milestone->update(false);
            SPF::flash(SiTrack::MSG_SAVED, 'success');
            SPF::redirect('projects', 'edit', $project->id);
         }

         SPF::flash(SiTrack::MSG_ERRORS, 'error');
         
      } // is_submitted

      $this->assign('project', $project->to_array());
      $this->assign('milestone', $milestone->to_array());
      
      $this->display('edit_milestone');

   } // edit_milestone
   
   public function delete( $id = NULL ) {
      
      // load the request object
      if( $id ) {
         $project = new Project();
         $project->delete( (int) $id );
         SPF::flash(SiTrack::MSG_DELETED, 'success');
      }
      
      SPF::redirect('projects');
      
   } // delete
   
   public function delete_component( $project_id = NULL, $id = NULL ) {
      
      $project = new Project();
      
      if( !$project->exists($project_id) )
         SPF::redirect('projects');
      
      if( $id ) {
         $component = new Component();
         $component->delete( (int) $id );
         SPF::flash(SiTrack::MSG_DELETED, 'success');
      }
      
      SPF::redirect('projects', 'edit', $project_id);

   } // delete_component
   
   public function delete_milestone( $project_id = NULL, $id = NULL ) {
      
      $project = new Project();

      if( $project->exists($project_id) && $id ) {
         $milestone = new Milestone();
         $milestone->delete( (int) $id );
         SPF::flash(SiTrack::MSG_DELETED, 'success');
      }
      
      SPF::redirect('projects', 'edit', $project_id);

   } // delete_milestone
   
} // Projects_Controller

?>
